package com.cc.moxingvoice.ui.main;

import android.content.pm.PackageManager;
import android.databinding.DataBindingUtil;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import com.cc.moxingvoice.R;
import com.cc.moxingvoice.base.BaseActivity;
import com.cc.moxingvoice.databinding.ActivityMainBinding;
import com.cc.moxingvoice.lock.MainLock;

import java.util.List;

/**
 * 创建日期：2018/2/23 on 8:39
 * 描述：
 * 作者：郭士超
 * QQ：1169380200
 */

public class MainActivity extends BaseActivity {

    private ActivityMainBinding mBinding;
    private MainLock mLock;

    private long exitTime = 0;

    @Override
    protected void initBinding() {
        // 数据绑定操作，可以套用代码
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mLock = new MainLock(this, mBinding, mBundle);
        mBinding.setLock(mLock);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 1:
                mLock.onRequestPermissionsResult(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED);
                break;
            default:
        }
    }

    @Override
    protected void init() {
        
    }

    public void voicePackageRefreshList() {
        mLock.voicePackageRefreshList();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (notInView(mBinding.llStartType, ev) && notInView(mBinding.tvStart, ev)) {
                    mLock.hideStartType();
                }
                break;
            case MotionEvent.ACTION_MOVE:
//                mLock.hideStartType();
                break;
            case MotionEvent.ACTION_UP:
//                mLock.hideStartType();
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    private boolean notInView(View v, MotionEvent event) {
        int[] l = {0, 0};
        v.getLocationInWindow(l);
        int left = l[0],
                top = l[1],
                bottom = top + v.getHeight(),
                right = left + v.getWidth();
        if (event.getX() > left && event.getX() < right
                && event.getY() > top && event.getY() < bottom) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!mLock.onHttpBack()) {
                exit();
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        long newTime = System.currentTimeMillis();
        if ((newTime - exitTime) > 2 * 1000) {
            showToast(getString(R.string.again_sgin_out));
            exitTime = newTime;
        } else {
            showToast(getString(R.string.app_exit));
            finish();
            System.exit(0);
        }
    }

    public void displayFragment(List<Fragment> fragments, Fragment fragment, FragmentManager fm,
                                int containerViewId) {
        FragmentTransaction ft = fm.beginTransaction();
        hideFragments(fragments, ft);
        if (fragment.isAdded()) {
            ft.show(fragment);
            ft.commit();
        } else {
            ft.add(containerViewId, fragment);
            ft.show(fragment);
            ft.commit();
        }
    }

    public void hideFragments(List<Fragment> fragments, FragmentTransaction ft) {
        for (int i = 0; i < fragments.size(); i++) {
            if (fragments.get(i) != null) {
                ft.hide(fragments.get(i));
            }
        }
    }

}
